package com.bawei.property.view

import android.graphics.BitmapFactory
import android.net.Uri
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.property.R
import com.bawei.property.databinding.ActivityParkDetailBinding
import com.bawei.property.entity.ParkValue
import com.blankj.utilcode.util.LogUtils
import com.google.vr.sdk.widgets.pano.VrPanoramaEventListener
import com.google.vr.sdk.widgets.pano.VrPanoramaView
import com.google.vr.sdk.widgets.video.VrVideoView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

@Route(path = RoutePath.PATH_PARK_DETAIL_PAGE)
class ParkDetailActivity : BaseMviActivity<ActivityParkDetailBinding>() {

    @Autowired
    lateinit var parkEntity: ParkValue

    override fun getLayoutId(): Int = R.layout.activity_park_detail

    override fun initView() {
        ARouter.getInstance().inject(this)
        mDataBinding.includePageTitle.ivBack.setOnClickListener {
            this@ParkDetailActivity.finish()
        }
        mDataBinding.includePageTitle.tvAction.run {
            visibility = View.VISIBLE
            text = "修改"
            setOnClickListener {
                ARouter.getInstance().build(RoutePath.PATH_EDIT_PARK_PAGE).withSerializable("parkEntity", parkEntity).navigation()
            }
        }

        //设置Google VR相关参数
        mDataBinding.vrPanoramaView.run {
            setInfoButtonEnabled(false)
            setFullscreenButtonEnabled(false)
            //设置事件监听
            setEventListener(object : VrPanoramaEventListener(){
                override fun onLoadSuccess() {
                    LogUtils.i("Vr图片加载成功！！")
                }

                override fun onLoadError(errorMessage: String?) {
                   showToastMsg("Vr图片加载失败！")
                }
            })
        }

        //加载Vr图片
//        loadLocalVrImage()
        loadLocalVrVideo()
    }

    private fun loadLocalVrImage() {
        mDataBinding.vrPanoramaView.visibility = View.VISIBLE
        mDataBinding.vrVideoView.visibility = View.GONE

        lifecycleScope.launch(Dispatchers.IO) {
            try {
                //从本地工程目录中加载VR图片
                var inputStream = assets.open("che.png")
                var vrBitmap = BitmapFactory.decodeStream(inputStream)
                var vrOptions = VrPanoramaView.Options()
                vrOptions.inputType = VrPanoramaView.Options.TYPE_MONO
                mDataBinding.vrPanoramaView.loadImageFromBitmap(vrBitmap, vrOptions)
            } catch (e: Exception){
                e.printStackTrace()
            } finally {
                cancel()
            }
        }
    }


    private fun loadLocalVrVideo(){
        mDataBinding.vrPanoramaView.visibility = View.GONE

        mDataBinding.vrVideoView.run {
            visibility = View.VISIBLE
            setInfoButtonEnabled(false)
            setFullscreenButtonEnabled(false)
        }

        lifecycleScope.launch(Dispatchers.IO) {
            try {
                var videoOptions = VrVideoView.Options()
                videoOptions.inputType = VrVideoView.Options.TYPE_MONO
                mDataBinding.vrVideoView.loadVideoFromAsset("video.mp4", videoOptions)
            }catch (e:Exception){
                e.printStackTrace()
            }finally {
                cancel()
            }
        }
    }


}